Problem 2277 Change
Accept: 245 Submit: 1186
Time Limit: 2000 mSec Memory Limit : 262144 KB
Problem Description
There is a rooted tree with n nodes, number from 1-n. Root’s number is 1.Each node has a value ai.
Initially all the node’s value is 0.
We have q operations. There are two kinds of operations.
1 v x k : a[v]+=x , a[v’]+=x-k (v’ is child of v) , a[v’’]+=x-2*k (v’’ is child of v’) and so on.
2 v : Output a[v] mod 1000000007(10^9 + 7).
Input
First line contains an integer T (1 ≤ T ≤ 3), represents there are T test cases.
In each test case:
The first line contains a number n.
The second line contains n-1 number, p2,p3,…,pn . pi is the father of i.
The third line contains a number q.
Next q lines, each line contains an operation. (“1 v x k” or “2 v”)
1 ≤ n ≤ 3*10^5
1 ≤ pi < i
1 ≤ q ≤ 3*10^5
1 ≤ v ≤ n; 0 ≤ x < 10^9 + 7; 0 ≤ k < 10^9 + 7
Output
For each operation 2, outputs the answer.
Sample Input
1 3 1 1 3 1 1 2 1 2 1 2 2
Sample Output
2 1
Source
题意:给你一棵树 有两种操作:查询节点的值,和将所有树节点及以下下所有的节点 + x - (子节点深度-当前深度)*k 的值
题解:首先肯定是DFS建序,然后根据dfs 序建一颗线段树,这题更新操作是更新一个区间,查询是单点。
这题只要用一个dep数组保存每个节点所包含区间里面的最小深度,然后向下更新的时候每次把,x,和k,更新下去;
更新的时候直接把,(子节点深度-当前深度)*k的值更新到 ,x,里面。
查询直接返回单点的x.
这题主要是卡取模和读入。。。读入特么就快超时了,最让我无语的还是超时给我返回一个WA加个读入挂就过了。。。。
1 | #include<iostream> |